﻿using EasyCaching.Core;
using EasyCaching.InMemory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyCaching.CSRedis;
using EasyCaching.Core.Configurations;
using MessagePack.Resolvers;
using Newtonsoft.Json;
using EasyCaching.SQLite;
using System.IO;

public static class EasyCachingExtensions
{
    /// <summary>
    /// 注册缓存服务,默认启用内存和csredis,使用MessagePack序列化(msgpack)
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddEasyCachingz(this IServiceCollection services, IConfiguration configuration, Action<InMemoryOptions> configureInMemoryOptions = default, Action<RedisOptions> configureRedisOptions = default, Action<SQLiteOptions> configureSQLiteOptions = default, Action<EasyCachingOptions> setupAction = default, bool useSQLite = false, bool useCSRedis = true)
    {
        services
            .AddEasyCaching(options =>
            {
                setupAction?.Invoke(options);
                options.UseInMemory(x =>
                {
                    x.SerializerName = "msgpack";
                    configureInMemoryOptions?.Invoke(x);
                    configuration.GetSection(EasyCachingConstValue.InMemorySection).Bind(x);
                    Console.WriteLine($"{EasyCachingConstValue.InMemorySection}配置--{JsonConvert.SerializeObject(x, new JsonSerializerSettings { Formatting = Formatting.Indented })}");
                });
                if (useCSRedis)
                {
                    options.UseCSRedis(x =>
                    {
                        x.SerializerName = "msgpack";
                        configureRedisOptions?.Invoke(x);
                        configuration.GetSection(EasyCachingConstValue.CSRedisSection).Bind(x);
                        Console.WriteLine($"{EasyCachingConstValue.CSRedisSection}配置--{JsonConvert.SerializeObject(x, new JsonSerializerSettings { Formatting = Formatting.Indented })}");
                    });
                }

                if (useSQLite)
                {
                    options.UseSQLite(x =>
                    {
                        x.SerializerName = "msgpack";
                        configureSQLiteOptions?.Invoke(x);
                        configuration.GetSection(EasyCachingConstValue.SQLiteSection).Bind(x);
                        //框架默认不会创建目录
                        if (!Directory.Exists(x.DBConfig.FilePath))
                        {
                            Directory.CreateDirectory(x.DBConfig.FilePath);
                        }
                        Console.WriteLine($"{EasyCachingConstValue.SQLiteSection}配置--{JsonConvert.SerializeObject(x, new JsonSerializerSettings { Formatting = Formatting.Indented })}");
                    });
                }
                //解决时间非本地时间的问题
                options.WithMessagePack(x =>
                {
                    x.EnableCustomResolver = true;
                    x.CustomResolvers = CompositeResolver.Create(NativeDateTimeResolver.Instance, ContractlessStandardResolver.Instance);
                }, "msgpack");
            });
        return services;
    }
}
